package com.zjsru.plan2023.oneday;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: CookLee
 * @Date: 2023/7/24
 * 宝石与石头
 * 输入：jewels = "aA", stones = "aAAbbbb"
 * 输出：3
 * /
 * 输入：jewels = "z", stones = "ZZ"
 * 输出：0
 */
public class NumJewelsInStones {
    
    /**
     * 暴力模拟
     *
     * @param jewels 珠宝
     * @param stones 石头
     * @return int
     */
    public int numJewelsInStones(String jewels, String stones) {
        int ans = 0;
        for (int i = 0; i < jewels.length(); i++) {
            char jewel = jewels.charAt(i);
            for (int j = 0; j < stones.length(); j++) {
                char stone = stones.charAt(j);
                if (stone == jewel) {
                    ans++;
                }
            }
        }
        return ans;
    }
    
    /**
     * 方法2 哈希map
     *
     * @param jewels 珠宝
     * @param stones 石头
     * @return int
     */
    public int numJewelsInStones2(String jewels, String stones) {
        int ans = 0;
        Set<Character> jewelMap = new HashSet<>();
        for (int i = 0; i < jewels.length(); i++) {
            jewelMap.add(jewels.charAt(i));
        }
        for (int i = 0; i < stones.length(); i++) {
            char stone = stones.charAt(i);
            if (jewelMap.contains(stone)) {
                ans++;
            }
        }
        return ans;
    }
    
    public static void main(String[] args) {
        NumJewelsInStones numJewelsInStones = new NumJewelsInStones();
        String jewels = "aA";
        String stone = "aAAbbbb";
        System.out.println(numJewelsInStones.numJewelsInStones(jewels, stone));
    }
}
